﻿using System;

using VirtualTournaments.DataAccess.SqlServer.DataMappers;

namespace VirtualTournaments.DataAccess.SqlServer.DataProxies
{
	internal sealed class MatchResultDataProxy : Business.Tournaments.MatchResult
	{
		private IDataProxy<SqlServer.Match> _dataProxy;

		internal MatchResultDataProxy(IDataProxy<SqlServer.Match> dataProxy)
			: base(null, null, Convert.ToByte(dataProxy.InnerDataObject.LocalGoals), Convert.ToByte(dataProxy.InnerDataObject.VisitorGoals))
		{
			this._dataProxy = dataProxy;
			MapProperties();
		}

		#region Override properties

		internal bool LocalHasBeenRead { get; set; }
		public override Business.Tournaments.Team Local
		{
			get
			{
				if (!LocalHasBeenRead)
				{
					LocalHasBeenRead = true;
					if (base.Local == null)
					{
						base.Local = MapLocal();
					}
				}
				return base.Local;
			}
		}

		internal bool VisitorHasBeenRead { get; set; }
		public override Business.Tournaments.Team Visitor
		{
			get
			{
				if (!LocalHasBeenRead)
				{
					LocalHasBeenRead = true;
					if (base.Visitor == null)
					{
						base.Visitor = MapVisitor();
					}
				}
				return base.Visitor;
			}
		}

		#endregion

		#region Mapping methods

		private void MapProperties()
		{
		}

		private Business.Tournaments.Team MapLocal()
		{
			return TeamsDataMapper.Instance.DataObject2BusinessObject(_dataProxy.InnerDataObject.LocalTeam);
		}

		private VirtualTournaments.Business.Tournaments.Team MapVisitor()
		{
			return TeamsDataMapper.Instance.DataObject2BusinessObject(_dataProxy.InnerDataObject.VisitorTeam);
		}

		#endregion
	}
}
